package com.audi.box.controller;

import java.util.ArrayList;

import android.content.Context;

import com.audi.box.adapter.SQLiteAsusHelperMember;
import com.audi.box.service.MemberService;
import com.audi.box.service.MemberServiceImp;
import com.audi.dto.MemberItem;

/**
 * @author CuongPH7
 * 
 */
public class MemberController implements IController {
    private MemberService memberService;
    
    /**
     * Class constructor specifying Context to create
     * 
     * @param android
     *            .content.Context context
     * @throws Exception
     *             if can not create MemberServiceImp
     */
    public MemberController(Context context) throws Exception {
        memberService = new MemberServiceImp(
                new SQLiteAsusHelperMember(context));
    }
    
    /**
     * Class constructor specifying MemberService to create
     * 
     * @param MemberService
     *            memberService
     * @see MemberService
     */
    public MemberController(MemberService memberService) {
        this.memberService = memberService;
    }
    
    /**
     * Inject MemberService
     * 
     * @param memberService
     *            MemberService
     * @see MemberService
     */
    public void setMemberService(MemberService memberService) {
        this.memberService = memberService;
    }
    
    /**
     * Add new Member
     * 
     * @param member
     *            MemberItem
     * @return Member Id if successful, 0 if member is exist
     * @throws Exception
     *             if member is null or member information missing
     */
    public int addMember(MemberItem member) {
        if (member == null) {
            throw new NullPointerException("Member is Null");
        }
        
        if (member.getMemberName() == null || member.getMemberName().equals("")
                || member.getMemberEmail() == null
                || member.getMemberEmail().equals("")) {
            throw new NullPointerException("Member Information is missing");
        }
        
        // Need to check Member name exist or email exist?
        return memberService.addMember(member);
        
    }
    
    /**
     * Add new Member by member information
     * 
     * @param memberName
     *            Member Name
     * @param memberEmail
     *            Member Email
     * @param memberPhoto
     *            Member Photo
     * @param memberStatus
     *            Member Status
     * @return Member Id if successful, 0 if member is exist
     * @throws Exception
     *             if member is null or member information missing
     */
    public int addMember(String memberName, String memberEmail,
            String memberPhoto, int memberStatus) {
        
        MemberItem member = new MemberItem();
        member.setMemberName(memberName);
        member.setMemberEmail(memberEmail);
        member.setMemberPhoto(memberPhoto);
        member.setMemberStatus(memberStatus);
        
        return addMember(member);
    }
    
    /**
     * Update Member
     * 
     * @param member
     *            MemberItem
     * @return 1 if successful, 0 if member is not exist
     * @throws Exception
     *             if member is null or member information missing
     */
    public int updateMember(MemberItem member) {
        if (member == null) {
            throw new NullPointerException("Member is Null");
        }
        
        if (member.getMemberId() == 0 || member.getMemberName() == null
                || member.getMemberName().equals("")
                || member.getMemberEmail() == null
                || member.getMemberEmail().equals("")) {
            throw new NullPointerException("Member Information is missing");
        }
        if (!memberService.isExist(member)) {
            return 0;
        } else {
            return memberService.updateMember(member);
        }
    }
    
    /**
     * Delete a Member
     * 
     * @param member
     *            MemberItem
     * @return 1 if successful, 0 if member is not exist
     */
    public int deleteMember(MemberItem member) {
        if (member == null) {
            throw new NullPointerException("Member is Null");
        }
        
        if (member.getMemberId() == 0) {
            throw new NullPointerException("Member Information is missing");
        }
        if (!memberService.isExist(member)) {
            return 0;
        } else {
            return memberService.deleteMember(member);
        }
    }
    
    /**
     * Get Member list
     * 
     * @return ArrayList Memeber Item
     */
    public ArrayList<MemberItem> getMemberList() {
        return memberService.getListMember();
    }
    
    /**
     * Check Member is Exist
     * 
     * @return <code>true</code> if Member is Exist <code>false</code> if Member
     *         is not Exist
     */
    public boolean isExist(MemberItem member) {
        if (member == null) {
            throw new NullPointerException("Member is Null");
        }
        return memberService.isExist(member);
    }
}
